package com.sprint.service.impl;

import com.sprint.dao.OrderDao;
import com.sprint.domain.Order;
import com.sprint.service.AccountService;
import com.sprint.service.OrderService;
import com.sprint.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
//@GlobalTransactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private StorageService storageService;
    @Autowired
    private AccountService accountService;


    @Override
    @GlobalTransactional(name="sprint-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("------------> 开始新建订单");
        orderDao.create(order);

        log.info("------------> 订单微服务开始调用库存，做扣减 数量");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("------------> 订单微服务开始调用库存，做扣减 end");

        log.info("------------> 订单微服务开始调用账户，做扣减 金额");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("------------> 订单微服务开始调用账户，做扣减 end");


        log.info("------------> 修改订单状态 开始");
        orderDao.update(order.getUserId(),1);
        log.info("------------> 修改订单状态 结束");

        log.info("------------> 新建订单结束");

    }
}
